<?php
 
namespace Custom\Controller; 

use Think\Controller; class RoleController extends UserController{
    public function __construct()
    {
        parent::__construct();
        $this->M_role = M('Role_son');
        $this->AuthModel = D('Auth');
    }

    public function index()
    {
        $where['c_id'] = $this->c_id;
        //获取所有的权限列表
        $this->Rolelist = $this->M_role->where($where)->select();
        $this->display();
    }
    //添加套餐
    public function add()
    {
        if(IS_POST){
            if(!$this->M_role->create()){
                $this->error('添加错误,错误为:'.$this->M_role->getError());die;
            }
            $this->M_role->c_id = $this->c_id;
            $res = $this->M_role->add();
            if($res){
                $this->success('添加成功',U('index'),2);die;
            }else{
                $this->error('添加失败!');die;
            }
        }
        $this->display();
    }

    public function setAuth()
    {
        $role_id = I('get.role_id',0,'intval');
        if ($role_id<1){
            $this->error('参数错误');die;
        }
        if (IS_POST){
            $_POST['role_auth_ids'] =implode(',',I('post.auth_id'));
            $where = "auth_pid!=0 AND auth_id IN ({$_POST['role_auth_ids']})";
            $current_auth = $this->AuthModel->getAuth($where);
            $auth_ac = '';
            foreach ($current_auth as $item){
                $auth_ac .= $item['auth_controller'].'-'.$item['auth_action'].',';
            }

            $_POST['role_auth_ac'] = rtrim( $auth_ac ,',');

            if(!$this->M_role->create()){
                $this->error($this->M_role->getError());die;
            }
            //保存权限
            $res = $this->M_role->save();

            if ($res){
                $this->success('分配权限成功',U('Role/index'),2);die;
            }else{
                $this->error('分配权限失败');die;
            }
        }
        $account_id = $this->account_id;
        $acc_res = M('Account')->where("account_id = $account_id")->find();
        $role = $acc_res['role_id'];
        // var_dump($acc_res['role_id']);exit();
        $limit = M('Role')->where("role_id = $role")->field('role_auth_ids')->find();

        // $this->topAuth = $this->AuthModel->getAuth("auth_pid=0");
        $this->topAuth = M('Auth')->where('auth_id in('.$limit['role_auth_ids'].') and auth_pid=0 ')->select();
        // $this->sonAuth = $this->AuthModel->getAuth("auth_pid!=0");
        $this->sonAuth = M('Auth')->where('auth_id in('.$limit['role_auth_ids'].') and auth_pid!=0')->select();
        // echo "<pre>";
        // var_dump($this->role_id,$limit,$this->topAuth);exit;
        $this->info = $this->M_role->find($role_id);
        $this->display();
    }
    //删除角色
    public function del()
    {
        $id = I('get.role_id');
        $res = $this->M_role->delete($id);
        $res?$this->success('删除成功',U('index'),1):$this->error('删除失败');
        $this->display();
    }
}